import Vue from "vue";
import VueStorage from "vue-ls";
import settings from "@/core/config/settings";

Vue.use(VueStorage, settings.storage);

export const setStorage = (key: any, value: any, timeOut: any = null) => {
    if (timeOut) {
        Vue.ls.set(key, value, timeOut);
    } else {
        Vue.ls.set(key, value);
    }
};
export const getStorage = (key: any) => Vue.ls.get(key);

/**
 * User-用户相关的KEY
 * 退出登陆后取消清除缓存
 */
export const UserKey = {
    Token: 'token',
    RefreshToken: 'RefreshToken',
    UserInfo: 'UserInfo',
    RoleInfo: 'RoleInfo',
    Roles: 'Roles',
    RoleCodes: 'RoleCodes',
    Menus: 'Menus',
    Resources: 'Resources',
    MenusPath: 'MenusPath',
};

//
export const setToken = (Token: any) => setStorage(UserKey.Token, Token);
export const getToken = () => getStorage(UserKey.Token);
export const removeToken = () => Vue.ls.remove(UserKey.Token);

//
export const setRefreshToken = (RefreshToken: any) => setStorage(UserKey.RefreshToken, RefreshToken);
export const getRefreshToken = () => getStorage(UserKey.RefreshToken);
export const removeRefreshToken = () => Vue.ls.remove(UserKey.RefreshToken);

//
export const setUserInfo = (UserInfo: any) => setStorage(UserKey.UserInfo, UserInfo);
export const getUserInfo = () => getStorage(UserKey.UserInfo);
export const removeUserInfo = () => getStorage(UserKey.UserInfo);

//
export const setRoleInfo = (UserInfo: any) => setStorage(UserKey.RoleInfo, UserInfo);
export const getRoleInfo = () => getStorage(UserKey.RoleInfo);
export const removeRoleInfo = () => getStorage(UserKey.RoleInfo);


//用户具备的动态路由对象
export const setMenus = (Menus: any) => setStorage(UserKey.Menus, Menus);
export const getMenus = () => {
    if (!getStorage(UserKey.Menus)) {
        return [];
    }
    return getStorage(UserKey.Menus)
};
export const removeMenus = () => Vue.ls.remove(UserKey.Menus);

//用户具备的动态路由对象
export const setResources = (Menus: any) => setStorage(UserKey.Resources, Menus);
export const getResources = () => {
    if (!getStorage(UserKey.Resources)) {
        return [];
    }
    return getStorage(UserKey.Resources)
};
export const removeResources = () => Vue.ls.remove(UserKey.Resources);

//用户具备的动态路由path集合
export const setMenusPath = (Menus: any) => setStorage(UserKey.MenusPath, Menus);
export const getMenusPath = () => {
    if (!getStorage(UserKey.MenusPath)) {
        return [];
    }
    return getStorage(UserKey.MenusPath);
};
export const removeMenusPath = () => Vue.ls.remove(UserKey.MenusPath);

//
export const removeAllCache = () => {
    for (let key in UserKey) {
        Vue.ls.remove((UserKey as any)[key])
    }
};


/**
 * Dict-字典缓存KEY
 * 退出登陆后没必要清除浏览器缓存
 */
export const DictKey = {
    AdminShowLog: 'AdminShowLog',
    AdminVersion: 'AdminVersion',
    ServerVersion: 'ServerVersion',
    BaseImageUrl: 'BaseImageUrl',
    WebSocketUrl: 'WebSocketUrl',
};
//
export const setWebSocketUrl = (webWebSocketUrl: string) => setStorage(DictKey.WebSocketUrl, webWebSocketUrl);
export const getWebSocketUrl = (): any => getStorage(DictKey.WebSocketUrl);
export const removeWebSocketUrl = () => Vue.ls.remove(DictKey.WebSocketUrl);
//
export const setAdminVersion = (version: string) => setStorage(DictKey.AdminVersion, version);
export const getAdminVersion = () => {
    if (getStorage(DictKey.AdminVersion)) {
        return getStorage(DictKey.AdminVersion);
    } else {
        return "0";
    }
};
export const removeAdminVersion = () => Vue.ls.remove(DictKey.AdminVersion);
//
export const setAdminShowLog = (value: string) => setStorage(DictKey.AdminShowLog, value);
export const getAdminShowLog = () => {
    let item = getStorage(DictKey.AdminShowLog);
    if (item) {
        return item == "true";
    }
    return false;
};
//
export const setServerVersion = (version: string) => setStorage(DictKey.ServerVersion, version);
export const getServerVersion = () => {
    if (getStorage(DictKey.ServerVersion)) {
        return getStorage(DictKey.ServerVersion);
    } else {
        return "0";
    }
};
export const removeServerVersion = () => Vue.ls.remove(DictKey.ServerVersion);

/**
 *App-服务运行依赖的KEY
 */
export const AppKey = {
    sidebarStatus: 'sidebarStatus',
    language: 'language',
    size: 'size',
};

export const getSidebarStatus = () => getStorage(AppKey.sidebarStatus);
export const setSidebarStatus = (sidebarStatus: string) => setStorage(AppKey.sidebarStatus, sidebarStatus);

export const getLanguage = () => getStorage(AppKey.language);
export const setLanguage = (language: string) => setStorage(AppKey.language, language);

export const getSize = () => getStorage(AppKey.size);
export const setSize = (size: string) => setStorage(AppKey.size, size);
